import request from "@/utils/request";
import { rootsUrl } from "../utils";
import { getToken } from "@/utils/auth";
import { parseTime } from "@/utils";
import Qs from "qs";
import { comment } from "postcss";

/**
 * 获取用户参与的活动列表
 * @param {Object} data
 */
export const getPersonActList = userId => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLecturePersonList.shtml`,
    method: "get",
    params: {
      mid: userId,
      token
    }
  });
};

/**
 * 获取用户参与的团队活动列表
 * @param {Object} data
 */
export const getTeamActList = (userId, pageIndex) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLectureTeamList.shtml`,
    method: "get",
    params: {
      mid: userId,
      token,
      pageIndex
    }
  });
};

export const cancelAct = (actId, userId, type) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/cancelReservation.shtml`,
    method: "get",
    params: {
      id: actId,
      mid: userId,
      type,
      token
    }
  });
};

/**
 * 获取活动列表
 * @param {*} date 活动日期
 * @param {*} pageIndex 页数
 */
export const getActList = (date, pageIndex, tagName, crowdId) => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLectureList.shtml`,
    method: "get",
    params: {
      pageIndex,
      sourceId: "WEB",
      lectureDate: date,
      pageSize: 10,
      tag: crowdId == 0 ? "" : crowdId,
      tagtype: tagName == "全部" ? "" : tagName
    }
  });
};

export const getActListByMonth = date => {
  let month = parseTime(date, "{y}-{m}");
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getlectureListByMonth.shtml`,
    method: "get",
    params: {
      month
    }
  });
};

/**
 * 获取预约类型与受众人群
 */
export const getActTag = () => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLectureTypeAndCrowd.shtml`,
    method: "get"
  });
};

/**
 * 获取预约活动详情
 * @param {Number} id
 */
export const getActDetail = id => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLectureDetails.shtml`,
    method: "get",
    params: {
      id
    }
  });
};

/**
 * 获取活动举行的有效日期
 * @param {Number} id
 */
export const getActDates = id => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecturePlay/getOpenDateList.shtml`,
    method: "get",
    params: {
      id
    }
  });
};

/**
 * 获取某一天举行的活动的时段。
 * @param {Number} id 活动ID
 * @param {*} date 选择的某一天 格式yyyy-mm-dd
 * @param {*} userId 用户ID
 */
export const getActPeriodInOneDay = (id, date, userId) => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecturePlay/getLecturePlayDates.shtml`,
    method: "get",
    params: {
      vid: id,
      lectureDate: date,
      mid: userId
    }
  });
};

/**
 * 活动预约--个人预约
 * @param {object} data
 */
export const personAppoint = data => {
  let param = { info: JSON.stringify(data) };
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/savePersonal.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(param)
  });
};

/**
 * 活动预约--获取个人预约年级列表
 */
export const getStudentGrades = () => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getStudentClass.shtml`,
    method: "get"
  });
};

/**
 * 获取活动评论列表
 * @param {*} id 活动ID
 */
export const getActCommentList = (id, mid) => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/lectureComment/getCommentList.shtml`,
    method: "get",
    params: {
      id,
      mid
    }
  });
};

/**
 * 活动预约 添加评论
 * @param {*} obj
 */
export const postComment = obj => {
  let token = getToken();
  let data = {
    token,
    vid: obj.id,
    mid: obj.userId,
    mname: obj.userName,
    content: obj.comment
  };
  return request({
    url: `${rootsUrl[1]}/cbeducate/lectureComment/addComment.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
};

/**
 * 评论点赞
 * @param {*} id 评论ID
 * @param {*} userId 用户ID
 */
export const likeComment = (id, userId, vvid) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/lectureComment/addCommentLikes.shtml`,
    method: "get",
    params: {
      id,
      token,
      mid: userId,
      vvid
    }
  });
};
